package com.lris.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lris.batch.FriendRelationsList;
import com.lris.constant.MessageConstant;
import com.lris.constant.StatusConstant;
import com.lris.dto.AddUserFriendDto;
import com.lris.exception.BaseException;
import com.lris.mapper.FriendRelationsMapper;
import com.lris.mapper.UserMapper;
import com.lris.pojo.FriendRelations;
import com.lris.pojo.User;
import com.lris.result.PageResult;
import com.lris.result.Result;
import com.lris.service.FriendRelationsService;
import com.lris.vo.UserFriendVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@RequiredArgsConstructor
@Slf4j
public class FriendRelationsServiceImpl implements FriendRelationsService {

    private final FriendRelationsMapper friendRelationsMapper;

    private final UserMapper userMapper;

    @Override
    public Result addUserFriend(AddUserFriendDto dto) {
        //添加者信息
        User addUser = userMapper.findUserById(dto.getAddFromId());


        //被添加者信息
        User toUser = userMapper.findUserById(dto.getToId());

        if (ObjectUtil.isNull(addUser) ||ObjectUtil.isNull(toUser) ) {
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (StatusConstant.DISABLE.equals(addUser.getStatus())||StatusConstant.DISABLE.equals(toUser.getStatus())) {
            return Result.error(MessageConstant.ACCOUNT_LOCKED);
        }
        //添加者
        FriendRelations add = FriendRelations.builder()
                .userId(addUser.getId())
                //被添加者id
                .friendId(toUser.getId())
                //添加时的申请好友备注信息
                .addRemark(dto.getRemark())
                //现关系
                .relation("1")
                //好有手机号
                .phone(toUser.getPhone())
                //好友昵称
                .friendName(toUser.getNickname())
                //当前时间
                .addDatetime(DateUtil.date().toLocalDateTime())
                .headImg(toUser.getHeadImg())
                .birthdate(null)
                .build();

        //被添加者

        FriendRelations to = FriendRelations.builder()
                .userId(toUser.getId())
                //被添加者id
                .friendId(addUser.getId())
                //添加时的申请好友备注信息
                .addRemark(dto.getRemark())
                //现关系
                .relation("1")
                //好有手机号
                .phone(addUser.getPhone())
                //好友昵称
                .friendName(addUser.getNickname())
                //当前时间
                .addDatetime(DateUtil.date().toLocalDateTime())
                .headImg(addUser.getHeadImg())
                .birthdate(null)
                .build();

        FriendRelationsList friendRelationsList = new FriendRelationsList();
        ArrayList<FriendRelations> list = new ArrayList<>();
        list.add(add);
        list.add(to);
        friendRelationsList.setFriendList(list);
        friendRelationsMapper.FriendBatchInsert(list);

        return Result.success(MessageConstant.ADD_FRIEND_SUCCESS);
    }

    /**
     * 查询好友列表
     * @param id 用户id
     * @return 好友列表
     */
    @Override
    public PageResult findFriendAllById( Integer page,Integer pageSize ,Long userid) {
        PageHelper.startPage(page, pageSize);
        Page<UserFriendVo> Page  =friendRelationsMapper.findFriendAllById(userid);
//        if(null==Page||Page.getResult().isEmpty()){
//           throw new BaseException(MessageConstant.FRIEND_IS_NULL);
//        }
        PageResult pageResult = new PageResult();
        pageResult.setRecords(Page.getResult());
        pageResult.setTotal(Page.getTotal());
        return pageResult;
    }
}
